package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.thread.AdminThreadLocalUtil;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ClassName: WmSensitiveServiceImpl
 * Description:
 * date: 2023/8/22 0022 15:33
 *
 * @author zHan
 */
@Service
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    /**
     * 查询敏感词列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmSensitiveDto dto) {
        // 检查参数
        dto.checkParam();
        // 查询是否登录
        AdUser adUser = AdminThreadLocalUtil.getUser();
        if (adUser == null) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 分页查询
        IPage<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        page = page(page, Wrappers.<WmSensitive>lambdaQuery()
                .like(StringUtils.isNotEmpty(dto.getName()), WmSensitive::getSensitives,
                        dto.getName())
                .orderByDesc(WmSensitive::getCreatedTime));
        // 封装对象
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(),
                (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 新增敏感词
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        // 参数校验
        WmSensitive dbSensitive = getOne(Wrappers.<WmSensitive>lambdaQuery()
                .eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        // 已存在的敏感词，不能保存
        if (dbSensitive != null) {
            return ResponseResult.errorResult(201, "敏感词已存在，新增失败");
        }
        // 补全参数
        wmSensitive.setCreatedTime(new Date());
        // 新增敏感词
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        // 直接删除
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        // 参数校验
        WmSensitive dbSensitive = getOne(Wrappers.<WmSensitive>lambdaQuery()
                .eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        // 已存在的敏感词，不能修改
        if (dbSensitive != null) {
            return ResponseResult.errorResult(201, "敏感词已存在，修改失败");
        }
        // 修改敏感词
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
